package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.BookModel;
import com.hangzhou.xyj.ui.widget.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by neo.duan on 2016/1/6.
 * 圈子头部
 */
public class BookHeadersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<BookModel> mDatas;

    public BookHeadersAdapter(Context context, List<BookModel> books) {
        this.mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mDatas = books;
    }

    /**
     * @param
     * @return
     * @Description 更新数据
     * @author yaohuix
     * @date 2016/3/3 10:13
     */
    public void update(List<BookModel> list) {
        if (list == null) {
            mDatas = new ArrayList<>();
        }
        this.mDatas = list;
        notifyDataSetChanged();
    }

    @Override
    public long getHeaderId(int position) {
        return mDatas.get(position).getType();
    }

    /**
     * item headerView的创建
     *
     * @param viewGroup the view to create a header view holder for
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        return new HeaderViewHolder(mLayoutInflater.inflate(R.layout.layout_book_item_header, viewGroup, false));
    }

    /**
     * 头部数据绑定
     *
     * @param holder   the view holder
     * @param position the adapter position
     */
    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((HeaderViewHolder) holder).setData(position);
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {
        private MyTextView mTvHeader;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            mTvHeader = (MyTextView) itemView.findViewById(R.id.tv_header_book);
        }

        public void setData(int position) {
            mTvHeader.setText(BookModel.TYPE.get(mDatas.get(position).getType()));
        }

    }

    /**
     * 正常条目item个数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        HashSet<Integer> set = new HashSet<Integer>();
        for (BookModel book : mDatas) {
            if (book.getType() != 0)
                set.add(book.getType());
        }
        return set.size();
    }

    /**
     * View类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return mDatas.get(position).getType();
    }

    /**
     * 正常条目item创建
     *
     * @param viewGroup
     * @param viewType  view的类型
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            default:
                viewHolder = new BookItemViewHolder(mLayoutInflater.inflate(R.layout.layout_book_item, viewGroup, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        //设置item数据
        if (viewHolder == null) {
            return;
        }
        if (viewHolder instanceof BookItemViewHolder) {
            ((BookItemViewHolder) viewHolder).setData(mDatas.get(i));
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.getInstance().goWebActivity(mContext, mDatas.get(i).getUrl());
                }
            });
        }
    }

    class BookItemViewHolder extends RecyclerView.ViewHolder {
        MyImageView mIvBookImg;
        MyTextView mTvBookName;
        MyTextView mTvIntroduce;
        MyTextView mTvLevel;

        public BookItemViewHolder(View itemView) {
            super(itemView);
            mIvBookImg = (MyImageView) itemView.findViewById(R.id.tv_book_img);
            mTvBookName = (MyTextView) itemView.findViewById(R.id.tv_book_name);
            mTvLevel = (MyTextView) itemView.findViewById(R.id.mtv_level);
            mTvIntroduce = (MyTextView) itemView.findViewById(R.id.mtv_introduce);
        }

        public void setData(BookModel data) {
            mIvBookImg.displayImage(data.getImage());
            mTvBookName.setText(data.getName());
            mTvLevel.setText(BookModel.LEVEL.get(data.getLevel()));
            mTvIntroduce.setText(data.getBookName());
        }
    }
}
